package cn.ecpay.unionpay.settle.notify;

import cn.ecpay.unionpay.settle.Common.DateUtil;
import cn.ecpay.unionpay.settle.dao.TuiHuiDao;
import cn.ecpay.unionpay.settle.entity.db.TPvTuiHuiNotify;
import cn.ecpay.unionpay.settle.util.CommonBase;
import cn.ecpay.unionpay.settle.util.FsasService;
import cn.ecpay.unionpay.settle.util.LogUtil;
import org.rex.db.exception.DBException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 退汇通知处理
 * Created by zhaoyanzeng on 2018/11/6.
 */
@Component
public class TuiHuiNotify {

    @Resource
    private TuiHuiDao tuiHuiDao;

    public void tuiHuiNotify(Map<String,String> reqParam)
            throws ServletException, IOException {
        LogUtil.writeLog("退汇接收来账通知开始");
        LogUtil.printRequestLog(reqParam);
        TPvTuiHuiNotify tuiHuiNotify = convertReturnMoney(reqParam);
        if (!FsasService.validate(reqParam, CommonBase.encoding)) {
            LogUtil.writeLog("验证签名结果[失败].");
            tuiHuiNotify.setNotifyValidate("0");
            //验签失败，需解决验签问题
        } else {
            LogUtil.writeLog("验证签名结果[成功].");
            tuiHuiNotify.setNotifyValidate("1");
            String respCode = reqParam.get("respCode"); //获取后台通知的数据，其他字段也可用类似方式获取
            LogUtil.writeLog("respCode=" + respCode);
        }
        try {
            tuiHuiDao.insert(tuiHuiNotify);
        } catch (DBException e) {
            LogUtil.writeLog("退汇通知保存失败");
            e.printStackTrace();
        }
        LogUtil.writeLog("接收退汇通知结束");
    }
    private TPvTuiHuiNotify convertReturnMoney(Map<String, String> reqParam) {
        TPvTuiHuiNotify tuiHuiNotify = new TPvTuiHuiNotify();
        tuiHuiNotify.setTxnNo(reqParam.get("txnNo"));
        tuiHuiNotify.setTxnType(reqParam.get("txnType"));
        tuiHuiNotify.setAcqInsCode(reqParam.get("acqInsCode"));
        tuiHuiNotify.setTxnDate(reqParam.get("txnDate"));
        tuiHuiNotify.setPayerBankNo(reqParam.get("payerBankNo"));
        tuiHuiNotify.setPayerAcctNo(reqParam.get("payerAcctNo"));
        tuiHuiNotify.setPayerAcctName(reqParam.get("payerAcctName"));
        tuiHuiNotify.setPayeeAcctNo(reqParam.get("payeeAcctNo"));
        tuiHuiNotify.setPayeeAcctName(reqParam.get("payeeAcctName"));
        tuiHuiNotify.setCurrencyCode(reqParam.get("currencyCode"));
        tuiHuiNotify.setTxnAmt(reqParam.get("txnAmt"));
        tuiHuiNotify.setPayeeAcctBal(reqParam.get("payeeAcctBal"));
        tuiHuiNotify.setAcctDate(reqParam.get("acctDate"));
        tuiHuiNotify.setRemark(reqParam.get("remark"));
        tuiHuiNotify.setOrigTxnNo(reqParam.get("origTxnNo"));
        tuiHuiNotify.setOrigTxnDate(reqParam.get("origTxnDate"));
        tuiHuiNotify.setMerId(reqParam.get("merId"));
        tuiHuiNotify.setMerName(reqParam.get("merName"));
        tuiHuiNotify.setCreateTime(DateUtil.getDateTo19(new Date()));
        return tuiHuiNotify;
    }
}
